package com.huatai.manage.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.huatai.common.util.StringUtil;
import com.huatai.manage.entity.TbMessageTopicEntity;
import com.huatai.manage.entity.TbTrialSubjectEntity;
import com.huatai.manage.mapper.TbMessageTopicMapper;
import com.huatai.manage.query.PageQuery;
import com.huatai.manage.service.TbMessageTopicService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huatai.manage.vo.ResultVo;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 消息主题管理; 服务实现类
 * </p>
 *
 * @author luoxc
 * @since 2023-03-28
 */
@Service
public class TbMessageTopicServiceImpl extends ServiceImpl<TbMessageTopicMapper, TbMessageTopicEntity> implements TbMessageTopicService {

	/**
	 * 分页获取主题列表
	 * 若传主题名称和主题类型，则根据主题名称模糊查询   主题类型精确查询
	 *
	 * @param pageQuery
	 * @return
	 */
	@Override
	public Page<TbMessageTopicEntity> pageList(PageQuery<TbMessageTopicEntity> pageQuery) {
		Page<TbMessageTopicEntity> page = new Page<>(pageQuery.getCurrent(), pageQuery.getSize());
		LambdaQueryWrapper<TbMessageTopicEntity> wrapper = Wrappers.<TbMessageTopicEntity>lambdaQuery();
		String topicName = pageQuery.getData().getTopicName();
		String topicType = pageQuery.getData().getTopicType();
		wrapper.like(StringUtil.isNotBlank(topicName), TbMessageTopicEntity::getTopicName, topicName);
		wrapper.eq(StringUtil.isNotBlank(topicType), TbMessageTopicEntity::getTopicType, topicType);
		return page(page, wrapper);
	}

	/**
	 * 根据主题id获取获取主题信息
	 *
	 * @param topicId
	 * @return
	 */
	@Override
	public TbMessageTopicEntity getByTopicId(String topicId) {
		return getOne(Wrappers.<TbMessageTopicEntity>lambdaQuery().eq(TbMessageTopicEntity::getTopicId, topicId));
	}

	/**
	 * 根据主题id集合删除主题信息
	 *
	 * @param topicIds
	 * @return
	 */
	@Override
	public boolean removes(List<String> topicIds) {
		if (topicIds.isEmpty()) return false;
		LambdaQueryWrapper<TbMessageTopicEntity> wrapper = Wrappers.<TbMessageTopicEntity>lambdaQuery();
		wrapper.in(TbMessageTopicEntity::getTopicId, topicIds);
		return remove(wrapper);
	}

	/**
	 * @return boolean
	 * @Description 根据主题id更新数据
	 * @Date 2023/4/4 10:14
	 * @Param messageTopicEntity
	 */
	@Override
	public boolean update(TbMessageTopicEntity messageTopicEntity) {
		LambdaUpdateWrapper<TbMessageTopicEntity> wrapper = Wrappers.<TbMessageTopicEntity>lambdaUpdate();
		wrapper.eq(TbMessageTopicEntity::getTopicId, messageTopicEntity.getTopicId());
		return update(messageTopicEntity, wrapper);
	}

	/**
	 * @return List<ResultVo>
	 * @Description 查询所有数据(壹瞰思)
	 * @Date 2023/4/4 10:15
	 * @Param null
	 */
	@Override
	public List<ResultVo> selectList() {
		List<ResultVo> resultVos = new ArrayList<>();
		List<TbMessageTopicEntity> list = this.list();
		if (list.isEmpty()) return null;
		list.forEach(i -> {
			resultVos.add(new ResultVo(i.getTopicId(), i.getTopicName()));
		});
		return resultVos;
	}
}
